package com.hiss.basic.starter.core.helper;


import cn.hutool.extra.spring.SpringUtil;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.Set;

/**
 * 数据校验工具类
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ValidationHelper {

    private static final Validator validator = SpringUtil.getBean(Validator.class);

    public static void validate(Object object, Class<?>... groups) throws ConstraintViolationException {
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
        if (!constraintViolations.isEmpty()) {
            throw new ConstraintViolationException(constraintViolations);
        }
    }
}
